package com.atguigu.tingshu.album.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 专辑数据同步rabbitmq的配置
 *
 * @author Taeyang
 * @date 2025/4/28
 */
@Configuration
public class AlbumSearchRabbitConfig {
    /**
     * 交换机初始化
     *
     * @return
     */
    @Bean("searchExchange")
    public Exchange searchExchange() {
        return ExchangeBuilder.topicExchange("search_exchange").build();
    }


    /**
     * 上架队列初始化
     *
     * @return
     */
    @Bean("searchUpperQueue")
    public Queue searchUpperQueue() {
        return QueueBuilder.durable("search_upper_queue").build();
    }

    /**
     * 下架队列初始化
     *
     * @return
     */
    @Bean("searchDownQueue")
    public Queue searchDownQueue() {
        return QueueBuilder.durable("search_down_queue").build();
    }

    /**
     * Redis同步队列
     *
     * @return
     */
    @Bean("redisQueue")
    public Queue redisQueue() {
        return QueueBuilder.durable("redis_queue").build();
    }


    /**
     * 交换机上架队列绑定
     *
     * @param searchExchange
     * @param searchUpperQueue
     * @return
     */
    @Bean
    public Binding searchUpperBinding(@Qualifier("searchExchange") Exchange searchExchange, @Qualifier("searchUpperQueue") Queue searchUpperQueue) {
        return BindingBuilder.bind(searchUpperQueue).to(searchExchange).with("album.upper").noargs();
    }

    /**
     * 交换机下架队列绑定
     *
     * @param searchExchange
     * @param searchDownQueue
     * @return
     */
    @Bean
    public Binding searchDownBinding(@Qualifier("searchExchange") Exchange searchExchange, @Qualifier("searchDownQueue") Queue searchDownQueue) {
        return BindingBuilder.bind(searchDownQueue).to(searchExchange).with("album.down").noargs();
    }

    /**
     * Redis同步队列绑定
     *
     * @param searchExchange
     * @param redisQueue
     * @return
     */
    @Bean
    public Binding redisBinding(@Qualifier("searchExchange") Exchange searchExchange, @Qualifier("redisQueue") Queue redisQueue) {
        return BindingBuilder.bind(redisQueue).to(searchExchange).with("album.#").noargs();
    }
}
